﻿/**
 * @author Ben.luo  luochengbang@163.com
 * reference:dwz.drag.js, dwz.dialogDrag.js, dwz.resize.js, dwz.taskBar.js
 *@参数说明：
 *selectedvalue [{id:'1233'}] 多个用and 定位
 */
(function ($) {
    $.selecterBen = {
        $p: $.pdialog.getCurrent() || navTab.getCurrentPanel(),
        _op: function ($this) {
            var op = {};
            var selectedstr = $this.attr("selectedvalue") || ""
            op.selectedvalue = selectedstr && selectedstr.length > 0 ? eval("(" + selectedstr + ")") : eval([]);;
            op.name = $this.attr("name");
            op.id = $this.attr("id");
            op.tabindex = $this.attr("tabindex") || -1;//tab回车键次
            op.ref = $this.attr("ref");//联动对象id
            op.refUrl = $this.attr("refUrl") || "";//联动ajax地址
            op.columns = $this.attr("columns") || "";//需要展示列
            op.datavalue = $this.attr("datavalue") || "";//保存值列
            op.datatext = $this.attr("datatext") || "";//显示文本列
            op.columnsjson = op.columns.length > 0 ? eval("(" + op.columns + ")") : eval([]);//列json对象
            op.filter = $this.attr("filter") || "";//过滤列名
            op.filttype = $this.attr("filttype") || "or";//过滤条件
            op.style = $this.attr("style") || "";//自定义样式
            op.readonly = $this.attr("readonly") || false;//只读不可编辑
            op.hiddenCols = ($this.attr("hiddenCols") || "") + ",";//隐藏列不显示在表格 多列用","分隔
            op.disabled = $this.attr("disabled") || false;//设置不可编辑            
            op.ajax = $this.attr("ajax") || "";//加载ajax数据
            op.data_tt_id = $this.attr("data-tt-id") || "";//tree table id
            op.data_tt_parent_id = $this.attr("data-tt-parent-id") || "";//tree table parent id
            op.pk = $this.attr("pk") || "Id";//行主键
            return op;
        },
        render: function (options, jsonData) {
            //var $this = $(this).removeClass("selecterBen");
            //$this.addClass("selecterBen_");
            var op = $.extend({}, $.pdialog._op($this), options);
            $.selecterBen.killbox();
            $.selecterBen.bindtable();
            $.selecterBen.reposition();
        },
        //删除
        killbox: function () { var cid = $.selecterBen._op.id; $("#selecterBen_", $.selecterBen.$p).remove(); },
        hidebox: function () { var cid = $.selecterBen._op.id; $("#selecterBen_", $.selecterBen.$p).hide(); },
        bindtable: function (options, jsonData) {
            var cid = options.id;
            var $box = '<div class="selecterBen_' + cid + '" id="selecterBen_"' + cid + '" for="' + name + '" rel="div' + name + '">\r\n{table}</div>';
            var $table = '<table class="list" id="table_' + cid + '">\r\n<thead>\r\n<tr>{thead}</tr>\r\n</thead>\r\n<tbody>\r\n{tbody}\r\n</tbody>\r\n</table>';
            var colmuns = [];

            //绑定header
            var $headerth = null;
            if (columnsjson.length > 0) {
                for (var i = 0; i < coljson.length; i++) {
                    for (var key in coljson[i]) {
                        if (options.hiddenCols.indexOf(key + ",") < 0) {
                            $headerth += "<th class='nowrap' columnname='" + key + "'>" + coljson[i][key] + "</th>";
                            colmuns.push(key);
                        }
                    }
                }
            }
            //绑定body
            var data = jsonData && jsonData.length > 0 ? eval("(" + jsonData + ")") : eval([]);
            var $tbody = null;
            for (var p in data) {
                if (typeof (val.summary) == "object")
                { $tbody += bindtr(data[p], colmuns); }
            }
            $table = $table.replace("{thead}", $headerth).replace("{tbody}", $tbody);
            var $box = $box.replace("{table}", $table);
            return $($box)
            function bindtr(json, colmuns) {
                var tds = null;
                var pk = null;
                for (var i = 0; i < json.length; i++) {
                    pk = json[i][$.selecterBen._op.pk];
                    for (var key in colmuns) {
                        tds += "<td columnname='" + key + "'>" + json[i][key] + "</td>";
                    }
                }
                return "<tr pk='" + pk + "'>" + tds + "</tr>";
            }
        },
        reposition: function () {
            var $box = $("#selecterBen_" + $.selecterBen._op().id);
            var $this = $(this);
            //var fwidth = $this.width();
            //var swidth = $box.width();
            var contextHeight = $box.height();
            if (contextHeight > 5) {
                contextHeight = contextHeight + 5;
            }
            if (contextHeight > 300) {
                contextHeight = 300;
            }
            $box.css({ height: contextHeight + "px", "overflow-y": "auto" });

            var top = contextHeight + $this.height();//+box[0].offsetHeight-50;
            if (top > $(window).height() - 20) {
                top = contextHeight - 10 - $this.height();
            }
            var left = $this.offset().left;
            if (left + $this.width() > $(window).width() - 20) {
                left = $this.offset().left + selector.width();
            }
            $box.css({ top: top, left: left }).show();
        },


    };
})(jQuery);